<?php
namespace app\modules\index\services;

use app\common\services\BaseService;
use app\config\Constants;
use Carbon\Carbon;

class CalculateService extends BaseService{

    /**
     * 配送时间
     * @param $business_time
     * @return array
     */
    public static function makeDeliveryDate($business_time){
        $time = explode("-",$business_time);
        $data = [
            '自提'=>['堂食','外带'],
        ];
        for($i=0;$i<2;$i++){
            $start = Carbon::now()->addDays($i);
            $date = $start->format("m-d");
            $end = Carbon::now()->addDays($i)->setHour($time[1])->setMinute(0);
            $temp = [];
            //当前时间设置
            if($i == 0){
                //是否在营业时间内
                $temp[] = \Yii::t("app","立即送出");
                $start->addMinutes(15);
                if($start->hour < $time[0]){
                    $start->setHour($time[0]);
                }
                if($start->hour >= $time[1]){
                    continue;
                }
            }else{
                $start->setHour($time[0]);
                $start->setMinute(0);
            }

            for(;!$start->gte($end);$start->addMinutes(15)){
                $temp[] = $start->format("H:i");
            }
            $data[$date] = $temp;
        }
        return $data;
    }

    /**
     * 计算价格
     * @param $merchant_id
     * @param $goods_item
     * @return array|bool
     */
    public static function calc($merchant_id,$goods_item){
        $total_price = 0;
        $data = [];
        foreach($goods_item as $k=>$v){
            $attr_price = 0;
            $goods = GoodsService::getInfoById($merchant_id,$v['goods_id']);
            if(!$goods){
                self::setError(\Yii::t("app","商品:{goods_name},不存在!",['goods_name'=>$v['goods_name']]));
                return false;
            }
            if($goods->is_sale == Constants::GOODS_STATUS_OFFLINE){
                self::setError(\Yii::t("app","商品:{goods_name},已被下架!",['goods_name'=>$v['goods_name']]));
                return false;
            }
            $temp = [
                'goods_id'=>$goods->goods_id,
                'thumbnail'=>$goods->thumbnail,
                'goods_name'=>$goods->goods_name,
                'weight'=>$goods->weight,
                'goods_count'=>$v['goods_count'],
            ];

            if(isset($v['attr_ids'])){
                $attr_ids = $v['attr_ids'];
                sort($attr_ids);
                foreach($attr_ids as $vv){
                    $attr = GoodsAttributeService::search($vv,$v['goods_id'],null);
                    if(!$attr){
                        continue;
                    }
                    if($attr[0]['count'] - $v['goods_count'] <= 0){
                        self::setError(\Yii::t("app","商品:{goods_name},库存不足",['goods_name'=>$v['goods_name']]));
                        return false;
                    }
                    $attr_price += $attr[0]['attr_price'];
                    $temp['attr_ids'][] = $vv;
                    $temp['attr_names'][] = $attr[0]['attr_value'];
                }
            }

            $temp['price'] = $goods->price + $attr_price;
            $temp['total_price']= round($temp['price'] * $v['goods_count'],2);
            $total_price += round($temp['price'] * $v['goods_count'],2);

            $data[] = $temp;
        }

        return [
            'total_price'=>$total_price,
            'goods_item'=>$data
        ];
    }
}